package com.zdy.viewpager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.race604.flyrefresh.FlyRefreshLayout;
import com.zdy.viewpager.fly.ItemData;
import com.zdy.viewpager.fly.SampleItemAnimator;

public class Fly2Fragment extends Fragment implements
		FlyRefreshLayout.OnPullRefreshListener {

	private FragmentActivity activity;
	private FlyRefreshLayout mFlylayout;
	private RecyclerView mListView;

	private ItemAdapter mAdapter;

	private ArrayList<ItemData> mDataSet = new ArrayList<ItemData>();
	private Handler mHandler = new Handler();
	private LinearLayoutManager mLayoutManager;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_dfly1, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		activity = getActivity();
		int position = FragmentPagerItem.getPosition(getArguments());
		TextView title = (TextView) view.findViewById(R.id.item_title);
		title.setText(String.valueOf(position));
		mFlylayout = (FlyRefreshLayout) view.findViewById(R.id.fly_layout);

		mFlylayout.setOnPullRefreshListener(this);

//		mListView = (RecyclerView) view.findViewById(R.id.list);
//
//		mLayoutManager = new LinearLayoutManager(activity);
//		mListView.setLayoutManager(mLayoutManager);
//		mAdapter = new ItemAdapter(activity);
//
//		mListView.setAdapter(mAdapter);
//
//		mListView.setItemAnimator(new SampleItemAnimator());

	}

	private void initDataSet() {
		mDataSet.add(new ItemData(Color.parseColor("#76A9FC"),
				R.drawable.ic_assessment_white_24dp, "Meeting Minutes",
				new Date(2014 - 1900, 2, 9)));
		mDataSet.add(new ItemData(Color.GRAY, R.drawable.ic_folder_white_24dp,
				"Favorites Photos", new Date(2014 - 1900, 1, 3)));
		mDataSet.add(new ItemData(Color.GRAY, R.drawable.ic_folder_white_24dp,
				"Photos", new Date(2014 - 1900, 0, 9)));
	}

	private void addItemData() {
		ItemData itemData = new ItemData(Color.parseColor("#FFC970"),
				R.drawable.ic_smartphone_white_24dp, "Magic Cube Show",
				new Date());
		mDataSet.add(0, itemData);
		mAdapter.notifyItemInserted(0);
		mLayoutManager.scrollToPosition(0);
	}

	@Override
	public void onRefresh(FlyRefreshLayout view) {
		View child = mListView.getChildAt(0);
		if (child != null) {
			bounceAnimateView(child.findViewById(R.id.icon));
		}

		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mFlylayout.onRefreshFinish();
			}
		}, 2000);
	}

	private void bounceAnimateView(View view) {
		if (view == null) {
			return;
		}

		Animator swing = ObjectAnimator.ofFloat(view, "rotationX", 0, 30, -20,
				0);
		swing.setDuration(400);
		swing.setInterpolator(new AccelerateInterpolator());
		swing.start();
	}

	@Override
	public void onRefreshAnimationEnd(FlyRefreshLayout view) {
		addItemData();
	}

	private class ItemAdapter extends RecyclerView.Adapter<ItemViewHolder> {

		private LayoutInflater mInflater;
		private DateFormat dateFormat;

		public ItemAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			dateFormat = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT,
					Locale.ENGLISH);
		}

		@Override
		public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
			View view = mInflater.inflate(R.layout.view_list_item, viewGroup,
					false);
			return new ItemViewHolder(view);
		}

		@Override
		public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
			final ItemData data = mDataSet.get(i);
			ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
			drawable.getPaint().setColor(data.color);
			itemViewHolder.icon.setBackgroundDrawable(drawable);
			itemViewHolder.icon.setImageResource(data.icon);
			itemViewHolder.title.setText(data.title);
			itemViewHolder.subTitle.setText(dateFormat.format(data.time));
		}

		@Override
		public int getItemCount() {
			return mDataSet.size();
		}
	}

	private static class ItemViewHolder extends RecyclerView.ViewHolder {

		ImageView icon;
		TextView title;
		TextView subTitle;

		public ItemViewHolder(View itemView) {
			super(itemView);
			icon = (ImageView) itemView.findViewById(R.id.icon);
			title = (TextView) itemView.findViewById(R.id.title);
			subTitle = (TextView) itemView.findViewById(R.id.subtitle);
		}

	}
}
